<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - wxauto_Mgt Web管理界面</title>
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .login-container {
            background: white;
            border-radius: 15px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            padding: 40px;
            width: 100%;
            max-width: 400px;
        }
        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }
        .login-header h2 {
            color: #333;
            font-weight: 600;
            margin-bottom: 10px;
        }
        .login-header p {
            color: #666;
            margin: 0;
        }
        .form-control {
            border-radius: 10px;
            border: 2px solid #e1e5e9;
            padding: 12px 15px;
            font-size: 16px;
            transition: all 0.3s ease;
        }
        .form-control:focus {
            border-color: #667eea;
            box-shadow: 0 0 0 0.2rem rgba(102, 126, 234, 0.25);
        }
        .btn-login {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            border-radius: 10px;
            padding: 12px;
            font-size: 16px;
            font-weight: 600;
            color: white;
            width: 100%;
            transition: all 0.3s ease;
        }
        .btn-login:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }
        .btn-login:disabled {
            opacity: 0.6;
            transform: none;
            box-shadow: none;
        }
        .alert {
            border-radius: 10px;
            border: none;
        }
        .password-toggle {
            position: absolute;
            right: 15px;
            top: 50%;
            transform: translateY(-50%);
            background: none;
            border: none;
            color: #666;
            cursor: pointer;
            z-index: 10;
        }
        .password-input-container {
            position: relative;
        }
        .loading-spinner {
            display: none;
            margin-right: 10px;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-header">
            <h2><i class="fas fa-shield-alt"></i> 身份验证</h2>
            <p>请输入访问密码以继续</p>
        </div>

        <div id="alertContainer"></div>

        <form id="loginForm">
            <div class="mb-3">
                <label for="password" class="form-label">访问密码</label>
                <div class="password-input-container">
                    <input type="password" class="form-control" id="password" name="password" required>
                    <button type="button" class="password-toggle" onclick="togglePassword()">
                        <i class="fas fa-eye" id="passwordToggleIcon"></i>
                    </button>
                </div>
            </div>
            <button type="submit" class="btn btn-login" id="loginBtn">
                <span class="loading-spinner">
                    <i class="fas fa-spinner fa-spin"></i>
                </span>
                <span id="loginBtnText">登录</span>
            </button>
        </form>

        <div class="text-center mt-3">
            <small class="text-muted">
                <i class="fas fa-info-circle"></i> 
                如果未设置密码，将自动跳转到管理界面
            </small>
        </div>
    </div>

    <!-- Bootstrap 5 JS Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <!-- jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

    <script>
        // 检查是否需要密码验证
        async function checkAuthStatus() {
            try {
                const response = await fetch('/api/auth/status');
                const data = await response.json();
                
                if (data.code === 0 && !data.data.password_required) {
                    // 不需要密码，直接跳转到首页
                    window.location.href = '/';
                    return;
                }
            } catch (error) {
                console.error('检查认证状态失败:', error);
            }
        }

        // 显示提示信息
        function showAlert(message, type = 'danger') {
            const alertContainer = document.getElementById('alertContainer');
            const alertHtml = `
                <div class="alert alert-${type} alert-dismissible fade show" role="alert">
                    <i class="fas fa-${type === 'success' ? 'check-circle' : 'exclamation-triangle'}"></i>
                    ${message}
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            `;
            alertContainer.innerHTML = alertHtml;
        }

        // 切换密码显示/隐藏
        function togglePassword() {
            const passwordInput = document.getElementById('password');
            const toggleIcon = document.getElementById('passwordToggleIcon');
            
            if (passwordInput.type === 'password') {
                passwordInput.type = 'text';
                toggleIcon.className = 'fas fa-eye-slash';
            } else {
                passwordInput.type = 'password';
                toggleIcon.className = 'fas fa-eye';
            }
        }

        // 设置加载状态
        function setLoading(loading) {
            const loginBtn = document.getElementById('loginBtn');
            const loadingSpinner = document.querySelector('.loading-spinner');
            const loginBtnText = document.getElementById('loginBtnText');
            
            if (loading) {
                loginBtn.disabled = true;
                loadingSpinner.style.display = 'inline-block';
                loginBtnText.textContent = '登录中...';
            } else {
                loginBtn.disabled = false;
                loadingSpinner.style.display = 'none';
                loginBtnText.textContent = '登录';
            }
        }

        // 处理登录表单提交
        document.getElementById('loginForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const password = document.getElementById('password').value;
            if (!password) {
                showAlert('请输入密码');
                return;
            }

            setLoading(true);
            
            try {
                const response = await fetch('/api/auth/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ password: password })
                });

                const data = await response.json();
                
                if (response.ok && data.code === 0) {
                    // 登录成功，Cookie已由服务器自动设置
                    showAlert('登录成功，正在跳转...', 'success');

                    // 将token保存到localStorage（与base.html的认证逻辑保持一致）
                    if (data.data && data.data.token) {
                        localStorage.setItem('auth_token', data.data.token);
                    }

                    // 延迟跳转，让用户看到成功提示
                    setTimeout(() => {
                        window.location.href = '/';
                    }, 1000);
                } else {
                    // 登录失败
                    showAlert(data.message || '登录失败');
                }
            } catch (error) {
                console.error('登录请求失败:', error);
                showAlert('网络错误，请稍后重试');
            } finally {
                setLoading(false);
            }
        });

        // 页面加载时检查认证状态
        document.addEventListener('DOMContentLoaded', function() {
            checkAuthStatus();
            
            // 聚焦到密码输入框
            document.getElementById('password').focus();
        });

        // 回车键提交表单
        document.getElementById('password').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                document.getElementById('loginForm').dispatchEvent(new Event('submit'));
            }
        });
    </script>
</body>
</html>
